﻿Imports TBCC.UtilityMobile
Public Class APNConfigForm

    Private _pi As ProjectItem
    Private _netid As Byte = g_netID
    Private _isModify As Boolean = False

    Private Const _Offset As UShort = 30
    Private Const _Interval As UShort = 10
    Private Const _Count As UShort = 3

    Private _Encoding As System.Text.Encoding = System.Text.Encoding.ASCII
    Private Const _DefaultPwd As String = "123456"

    Private Sub APNConfigForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.CenterToParent()
        SetModifyState(False)
        Me.btnModify.Enabled = False
        'Me.grpMain.Enabled = False

        _pi = Me.MyParentForm.PI

        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(False)
        End If

        Me.Timer1.Start()

    End Sub

    Private Sub APNConfigForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click

        Me.btnModify.Enabled = False

        If EnterModify() = False Then
            Me.btnModify.Enabled = True
            Exit Sub
        End If

        SetModifyState(True)
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim result As Boolean

        Me.Cursor = Cursors.WaitCursor
        result = Save()
        Me.Cursor = Cursors.Default
        If result Then
            SetModifyState(False)
            msg.Show("保存成功。")
        Else
            msg.ShowError("保存失败。")
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Timer1.Stop()
        If RefreshData() = True Then
            Me.btnModify.Enabled = True
        End If
        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(True)
        End If
    End Sub

    Private Sub txtAPN_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAPN.KeyPress, txtUserName.KeyPress, txtPwd.KeyPress
        Dim ascii As Integer

        ascii = Asc(e.KeyChar)

        If ascii >= 0 AndAlso ascii <= 255 Then
            e.Handled = False
        Else
            e.Handled = True
        End If

    End Sub

    Private Function RefreshData() As Boolean
        Dim buf() As Byte

        'GPRS连接参数
        buf = COMHelper.Instance.Send4Byte2(_netid, FunctionCode.WordWritableRead, _Offset, _Interval * _Count)
        If buf Is Nothing OrElse buf.Length <= 0 Then
            msg.ShowError(g_strReadError)
            Return False
        End If

        Me.txtAPN.Text = _Encoding.GetString(buf, 0, _Interval)
        Me.txtAPN.Tag = Me.txtAPN.Text

        Me.txtUserName.Text = _Encoding.GetString(buf, _Interval * 1, _Interval)
        Me.txtUserName.Tag = Me.txtUserName.Text

        If buf(_Interval * 2) > 0 Then
            Me.txtPwd.Text = _DefaultPwd
            Me.txtPwd.Tag = Me.txtPwd.Text
        Else
            Me.txtPwd.Text = String.Empty
            Me.txtPwd.Tag = Me.txtPwd.Text
        End If

        Return True
    End Function

    '设置控件修改状态
    Private Sub SetModifyState(ByVal flag As Boolean)

        Me.txtAPN.ReadOnly = Not flag
        Me.txtUserName.ReadOnly = Not flag
        Me.txtPwd.ReadOnly = Not flag

        Me.btnModify.Enabled = Not flag
        Me.btnOK.Enabled = flag
        Me.grpMain.Enabled = flag
    End Sub

    '开始修改
    Private Function EnterModify() As Boolean
        If g_test Then
            _isModify = True
            Return True
        End If


        Dim result As Boolean
        result = ParameterModifyClass.EnterModify(_netid)

        If result = False Then
            Return False
        End If

        ''开始发送心跳包
        'Me.TimerHeart.Start()

        _isModify = True

        Return True
    End Function

    '取消修改
    Private Function ExitModify() As Boolean
        Dim result As Boolean
        result = ParameterModifyClass.ExitModify(_netid)

        ''停止发送心跳包
        'Me.TimerHeart.Stop()

        _isModify = False

        Return True
    End Function

    '保存参数
    Private Function Save() As Boolean
        Dim result As Boolean
        Dim byteBuf() As Byte

        'APN
        If Me.txtAPN.Text <> Me.txtAPN.Tag Then '已经修改
            byteBuf = _Encoding.GetBytes(Me.txtAPN.Text)
            ReDim Preserve byteBuf(_Interval - 1)
            result = Save(_Offset, byteBuf)
            If result = False Then
                GoTo FailedHandler
            End If
        End If

        '用户名
        If Me.txtUserName.Text <> Me.txtUserName.Tag Then '已经修改
            byteBuf = _Encoding.GetBytes(Me.txtUserName.Text)
            ReDim Preserve byteBuf(_Interval - 1)
            result = Save(_Offset + _Interval * 1, byteBuf)
            If result = False Then
                GoTo FailedHandler
            End If
        End If

        '密码
        If Me.txtPwd.Text <> Me.txtPwd.Tag Then '已经修改
            byteBuf = _Encoding.GetBytes(Me.txtPwd.Text)
            ReDim Preserve byteBuf(_Interval - 1)
            result = Save(_Offset + _Interval * 2, byteBuf)
            If result = False Then
                GoTo FailedHandler
            End If
        End If

        '设置修改使能变量
        If ParameterModifyClass.SetModifyConfirm(_netid) = False Then
            Return False
        End If

        'If ExitModify() = False Then
        '    Debug.Write("ExitModify错误")
        'End If

        Return True

FailedHandler:
        If ExitModify() = False Then
            Debug.Write("ExitModify错误")
        End If
        Return False

    End Function

    Private Function Save(ByVal offset As UShort, ByVal value As Byte) As Boolean
        offset = offset + 1000
        Return COMHelper.Instance.Write(_netid, FunctionCode.WordWrite, offset, value)
    End Function

    Private Function Save(ByVal offset As UShort, ByVal value() As Byte) As Boolean
        For i As UShort = 0 To value.Length - 1
            If Save(offset + i, value(i)) = False Then
                Return False
            End If
        Next
        Return True
    End Function

End Class